package com.sudichina.sudichina.view.activity.wallet;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.common.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.DingDanShengChengData;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.entity.UserRecharge;
import com.sudichina.sudichina.pay.ConstantKeys;
import com.sudichina.sudichina.pay.OnPayListener;
import com.sudichina.sudichina.pay.PayAgent;
import com.sudichina.sudichina.pay.PayInfo;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.login.LoginActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sudichina.sudichina.R.id.et_recharge_money;

/**
 * Created by ${杨伟乔} on 2017/3/10.
 * 邮箱: yangweiqiao@126.com
 * QQ: 463217558
 * 别人光鲜的背后或者有着太多不为人知的痛苦
 * 学习可以改变一切、 行动可以解决一切问题、
 */

/**
 * 充值的界面
 */
public class RechargeActivity extends BaseActivity implements onRequestDataFinish {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(et_recharge_money)
    EditText etRechargeMoney;
    @BindView(R.id.tv_money_200)
    TextView tvMoney200;
    @BindView(R.id.tv_money_500)
    TextView tvMoney500;
    @BindView(R.id.tv_money_1000)
    TextView tvMoney1000;
    @BindView(R.id.tv_money_2000)
    TextView tvMoney2000;
    @BindView(R.id.tv_money_5000)
    TextView tvMoney5000;
    @BindView(R.id.tv_money_10000)
    TextView tvMoney10000;
    @BindView(R.id.tv_cup)
    TextView tvCup;
    @BindView(R.id.tv_alipay)
    TextView tvAlipay;
    @BindView(R.id.tv_wechat)
    TextView tvWechat;
    private String tradeId;
    private int types;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_recharge);
        ButterKnife.bind(this);
        //检测是否登录
        LoginActivity.startMeAndInitIsLogin(this, Contains.YOUKA_EXTRA);
        tvTitle.setText("充值");

    }


    @OnClick({R.id.iv_back, R.id.tv_money_200, R.id.tv_money_500, R.id.tv_money_1000, R.id.tv_money_2000, R.id.tv_money_5000, R.id.tv_money_10000, R.id.tv_cup, R.id.tv_alipay, R.id.tv_wechat})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.tv_money_200:
                etRechargeMoney.setText("200");
                etRechargeMoney.setSelection(etRechargeMoney.length());
                break;
            case R.id.tv_money_500:
                etRechargeMoney.setText("500");
                etRechargeMoney.setSelection(etRechargeMoney.length());
                break;
            case R.id.tv_money_1000:
                etRechargeMoney.setText("1000");
                etRechargeMoney.setSelection(etRechargeMoney.length());
                break;
            case R.id.tv_money_2000:
                etRechargeMoney.setText("2000");
                etRechargeMoney.setSelection(etRechargeMoney.length());
                break;
            case R.id.tv_money_5000:
                etRechargeMoney.setText("5000");
                etRechargeMoney.setSelection(etRechargeMoney.length());
                break;
            case R.id.tv_money_10000:
                etRechargeMoney.setText("10000");
                etRechargeMoney.setSelection(etRechargeMoney.length());
                break;
            case R.id.tv_cup://银联
                break;
            case R.id.tv_alipay://支付宝

                types = 1;

                /**
                 * 获取订单号
                 */
                HashMap<String, Object> map = new HashMap<>();

                map.put("accountId", MyApp.getCurrentUser().getId());

                map.put("money", etRechargeMoney.getText().toString().trim());

                Type type = new TypeToken<Result<DingDanShengChengData>>() {
                }.getType();

                NetUtils.requestData(Api.RECHARGE, map, this, type, false);


                break;
            case R.id.tv_wechat://微信
                types = 2;
                /**
                 * 获取订单号
                 */
                HashMap<String, Object> map1 = new HashMap<>();

                map1.put("accountId", MyApp.getCurrentUser().getId());

                map1.put("money", etRechargeMoney.getText().toString().trim());

                Type type1 = new TypeToken<Result<DingDanShengChengData>>() {
                }.getType();

                NetUtils.requestData(Api.RECHARGE, map1, this, type1, false);


                break;
        }
    }

    private void pay(String substring) {
        progressDialog = new ProgressDialog(RechargeActivity.this);
        PayInfo payInfo = new PayInfo();
        payInfo.setSubject("分通车主");
        payInfo.setPrice(etRechargeMoney.getText().toString().trim());
        payInfo.setNotifyUrl(ConstantKeys.AliPay.aliPay_notifyURL);
        payInfo.setBody("分通物流车主充值");
        payInfo.setOrderNo(substring);
        testPay(PayAgent.PayType.ALIPAY, payInfo);


    }

    /**
     * 微信支付
     */
    private void pay2(String substring) {
        progressDialog = new ProgressDialog(RechargeActivity.this);
        PayInfo payInfo = new PayInfo();
        payInfo.setSubject("分通车主");
        payInfo.setPrice(etRechargeMoney.getText().toString().trim());

        payInfo.setBody("分通物流车主充值");
        payInfo.setOrderNo(substring);
        testPay(PayAgent.PayType.WECHATPAY, payInfo);


    }


    private ProgressDialog progressDialog;

    private void testPay(PayAgent.PayType alipay, PayInfo payInfo) {

        PayAgent.getInstance().onPay(alipay, this, payInfo,
                new OnPayListener() {

                    @Override
                    public void onStartPay() {

                        progressDialog.setTitle("加载中...");
                        // progressDialog.show();
                    }

                    @Override
                    public void onPaySuccess() {
//						public void onPaySuccess(String code, String msg) {

                        Toastutils.showToast("支付成功！");
                        if (null != progressDialog) {
                            progressDialog.dismiss();
                        }
                        //services/tuserbill/updateRecharge/{type}

                        UserRecharge recharge = new UserRecharge();
                        recharge.setChannel("alipay");
                        recharge.setAccountId(MyApp.getCurrentUser().getId());
                        recharge.setStatus("success");
                        recharge.setTradeId(tradeId);
                        String type = "app_recharge";
                        NetUtils.requestDataformentity(Api.UPDATERECHARGE + type, recharge, new onRequestDataFinish() {
                            @Override
                            public void requestdataFinish(String catalog, Object bean) {
                                System.out.println("支付成功的:" + bean);
                            }

                            @Override
                            public void requestdataFailed(IOException e) {

                            }
                        }, null);


                    }

                    @Override
                    public void onPayFail(String code, String msg) {

                        Toastutils.showToast("支付失败！");
                        if (null != progressDialog) {
                            progressDialog.dismiss();
                        }
                    }
                });


    }


    @Override
    public void requestdataFinish(String catalog, Object bean) {
        Result<DingDanShengChengData> bean1 = (Result<DingDanShengChengData>) bean;

        int code = bean1.getCode();

        if (code == 1) {
            //正常的情况了
            DingDanShengChengData data = bean1.getData();
            tradeId = data.getTradeId();
            int i = tradeId.indexOf("_");
            String substring = tradeId.substring(i + 1);
            System.out.println("截取后的订单号是:" + substring);


            if (types == 1) {
                pay(substring);
            } else if (types == 2) {
                pay2(substring);
            }


        } else {

            Toastutils.showToast(bean1.getMsg());

        }
        // pay();


    }

    @Override
    public void requestdataFailed(IOException e) {

    }
}
